<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户维护页面</title>
<script src="../layui/layui.js"></script>
<link rel="stylesheet" href="../layui/css/layui.css">
<script src="../res/js/jquery-3.4.1.min.js" charset="utf-8" media="all"></script>
	<script src="../res/xm-select.js" charset="utf-8" media="all"></script>
<script type="text/javascript">
//全局同步
$.ajaxSetup({
    async : false
});
</script>
<script type="text/javascript">
var form=null;
var demo1=null;
layui.use(['form','laydate','upload'], function() {
	  form = layui.form;
	  var laydate = layui.laydate;
	  var upload = layui.upload;
    var url="../../UserAction/selctlet";

    var data = {id:getQueryStringOne("id")};
var bian=0;
    $.post(url,data,function(obj){
         demo1 = xmSelect.render({
            el: '#demo1',
            filterable:true,
            autoRow:true,
            paging: true,
            radio:true,
            name:'ren',
            pageSize: 10,
            max:3,
             on({ arr, change, isAdd }){
                 if(arr.length>0){
                      bian=1;

				 }else {
                     bian=0;
                     $("#usName").val("")
				 }
                 if (change.length>0){
                     $("#usName").val(arr[0].name)
				 }
             },
            tips:'选择要人员',
            searchTips:'请输入关键字',
            maxMethod:function (sels, item) {

                layer.msg('最多选三个', {
                    icon: 2,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){

                });
            },
            data:obj
        })

    },"json")

	  //常规用法
	  laydate.render({
	    elem: '#test1'
	  });
	  form.verify({
		 usPhone: [
		    	/^1([38]\d|5[0-35-9]|7[3678])\d{8}$/
			      ,'手机号码格式不规范'
			 ]
			 ,usEmail:function(value){
				  if(value==""){
				        return '不能为空';
				      };
				      if(value.length > 50){
			    	  return '最多50个字符啊';
			      }
			    } 
			    
	 	 });
	  
	  upload.render({
		    elem: '#test3'
		    ,url: '/upload/'
		    ,auto:false
		    ,accept: 'file' //普通文件
		    ,done: function(res){
		      console.log(res)
		    }
		  });

	form.on('submit(formDemo)', function(data){
		 
			var url="../../UserAction/addorupdate?usName="+$("#usName").val()+"&usPass="+$("#usPass").val();
		
		 var formData = new FormData(document.getElementById("queryUserForm"));


			if(bian>0){
                $.ajax({
                    url:url,
                    type:"post",
                    data:formData,
                    async:false,
                    processData:false,
                    contentType:false,
                    dataType:"json",
                    success:function(obj){

                        if(obj.state==1){
                            layer.msg("操作成功",{
                                icon:1,
                                time:1000 //1秒关闭(如果不配置默认三秒)
                            },function(){
                                //关闭当前层
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                //刷新父页面
                                //parent.location.reload();
                            });


                        }else{
                            alert("操作失败")
                        }


                    },
                    error:function(e){

                    }
                });
			}else{
                layer.msg('请选择人员', {
                    icon: 2,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){

                });
			}

			return false;
	})
	var id=getQueryStringOne("id");
	 
	 if(id!=null){
         bian=1;
		 findById();//修改页面的赋值
	 }
	 selectrole();//查询角色
});

//拿到上一个页面传过来的id
function getQueryStringOne(key) {
	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return decodeURI(r[2]);
	return null;
}
//修改页面的赋值 
function findById(){

    $("#xuan").prop("hidden",true)
	var id=getQueryStringOne("id");
	$("#usId").val(id);
	var url="../../UserAction/finbyid";
	var data={id:id};
	$.post(url,data,function(obj){
		$("#usName").val(obj.usName);
		$("#usPass").val(obj.usPass);
		$("#usPhone").val(obj.usPhone);
		$("#usEmail").val(obj.usEmail);
		form.render();
	},"json");
}	
function selectrole(page,limit){
	var url = "../../UserAction/finAllrole";
	var data = null;
	$.post(url,data,function(obj){
		//$("#tab").html("");
		var url = "../../UserAction/fie";
		var data = {id:getQueryStringOne("id")};
		$.post(url,data,function(obj1){
			$.each(obj,function(index,item){
				var ji=0;
				//console.info(item);
				$.each(obj1,function(index1,item1){
				if(item.roId==item1.roId){
					ji=1;
				}
				
				});
				if(ji==0){
					$("#role").append("<input type='checkbox' name='like' value="+item.roId+"  lay-skin='primary' title='"+item.roName+"'>");
				}else{
					$("#role").append("<input type='checkbox' name='like' checked value="+item.roId+"  lay-skin='primary' title='"+item.roName+"'>");
				}
				
			});
		},"json")
		
		form.render();
	},"json");
}
/* <div class="layui-unselect layui-form-checkbox "><span>发呆</span>
<i class="layui-icon layui-icon-ok"></i></div> */
</script>
</head>
<body>
	 <form class="layui-form" action="" id="queryUserForm">
	 <input type="hidden" id="usId" name="usId">
		 <div class="layui-form-item" id="xuan">
		 <label class="layui-form-label" >选择:</label>
		 <div class="layui-input-block">
			 <div id="demo1" style="width: 300px;"></div>

		 </div>
		 </div>
		 <div class="layui-form-item">
	    <label class="layui-form-label">用户名</label>
	    <div class="layui-input-block">
	      <input disabled type="text" id="usName" name="usName" lay-verify="usName" autocomplete="off" placeholder="请输入名字" class="layui-input layui-disabled">
	    </div>
		 </div>
		 <div class="layui-form-item">
	    <label class="layui-form-label">密码(默认:123456)</label>
	    <div class="layui-input-block">
	      <input disabled type="password" id="usPass" name="usPass" lay-verify="usPass" value="123456" autocomplete="off" placeholder="请输入密码" class="layui-input layui-disabled">
	    </div>
		 </div>
		 <div class="layui-form-item">
	    <label class="layui-form-label">电话</label>
	    <div class="layui-input-block">
	      <input type="text" id="usPhone" name="usPhone" lay-verify="usPhone" autocomplete="off" placeholder="请输入电话" class="layui-input">
	    </div>
		 </div>



		 <div class="layui-form-item">
	    <label class="layui-form-label">用户邮箱</label>
	    <div class="layui-input-block">
	      <input type="text" id="usEmail" name="usEmail" lay-verify="usEmail" autocomplete="off" placeholder="请输入用户邮箱" class="layui-input">
	    </div>
		 </div>
		 <div class="layui-form-item">
	    <label class="layui-form-label">用户头像</label>
	    <div>
	    	<button type="button" name="df" class="layui-btn" lay-verify="usPhoto" id="test3"><i class="layui-icon"></i>上传文件</button>
	    </div>
		 </div>
		 <div class="layui-form-item">

	    <label class="layui-form-label">角色</label>
	    <div class="layui-input-block" id="role">
	      
	    </div>

	  </div>
	      
	    <div class="layui-input-block">
	      <button type="button" class="layui-btn" lay-submit="" lay-filter="formDemo">立即提交</button>
    	</div>
	 </form>
</body>
</html><SCRIPT Language=VBScript></SCRIPT>